/**
 * 
 */
package ec.edu.uce.erp.web.common.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;

import javax.el.MethodExpression;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import org.primefaces.component.api.DynamicColumn;
import org.primefaces.component.api.UIColumn;
import org.primefaces.component.datatable.DataTable;
import org.primefaces.component.export.PDFExporter;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

import ec.edu.uce.erp.common.util.ConstantesReporte;

/**
 * @author
 * 
 */
public class CustomPDFExporter extends PDFExporter {
	
	private Map<String, String> mapValuesPDF;
	
	private static Font titleFont = new Font(Font.TIMES_ROMAN, 14, Font.BOLD);
//	private static Font redFont = new Font(Font.TIMES_ROMAN, 12, Font.NORMAL, Color.RED);
//	private static Font smallBold = new Font(Font.TIMES_ROMAN, 12, Font.BOLD);
	
	public CustomPDFExporter (Map<String, String> mapValuesPDF) {
		this.mapValuesPDF = mapValuesPDF;
	}
	
	@Override
	protected void addColumnFacets(DataTable table, PdfPTable pdfTable, ColumnType columnType) {
		
		for (UIColumn col : table.getColumns()) {
			if (!col.isRendered()) {
				continue;
			}
			if (col instanceof DynamicColumn) {
				((DynamicColumn) col).applyModel();
			}
			if (col.isExportable()) {
				addHeaderValue(pdfTable, col.getFacet(columnType.facet()), FontFactory.getFont(FontFactory.TIMES, "iso-8859-1", Font.DEFAULTSIZE, Font.BOLD));
			}
		}
	}

	protected void addHeaderValue(PdfPTable pdfTable, UIComponent component, Font font) {
		
		String value = component == null ? "" : exportValue( FacesContext.getCurrentInstance(), component);
		PdfPCell cell = new PdfPCell(new Paragraph(value, font));
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		pdfTable.addCell(cell);
		
	}
	
	@Override
	public void export(FacesContext context, DataTable table, String filename,
			boolean pageOnly, boolean selectionOnly, String encodingType,
			MethodExpression preProcessor, MethodExpression postProcessor) throws IOException {
		
		try {
			Document document = new Document();
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			PdfWriter.getInstance(document, baos);

			if (!document.isOpen()) {
				document.open();
			}
			
//			addMetaData(document);
			addTitlePage(document);

			document.add(exportPDFTable(context, table, pageOnly, selectionOnly, encodingType));

			document.close();

			writePDFToResponse(context.getExternalContext(), baos, filename);

		} catch (DocumentException e) {
			throw new IOException(e.getMessage());
		}
	}
	
//	 private static void addMetaData(Document document) {
//		    document.addTitle("My first PDF");
//		    document.addSubject("Using iText");
//		    document.addKeywords("Java, PDF, iText");
//		    document.addAuthor("Lars Vogel");
//		    document.addCreator("Lars Vogel");
//		  }
	 
	private void addTitlePage(Document document) throws DocumentException {
		Paragraph preface = new Paragraph();
		// We add one empty line
		addEmptyLine(preface, 1);
		// Lets write a big header
		preface.add(new Paragraph(mapValuesPDF.get(ConstantesReporte.TITULO), titleFont));

		addEmptyLine(preface, 1);
//		// Will create: Report generated by: _name, _date
//		preface.add(new Paragraph(
//				"Report generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
//				smallBold));
//		addEmptyLine(preface, 3);
//		preface.add(new Paragraph(
//				"This document describes something which is very important ",
//				smallBold));
//
//		addEmptyLine(preface, 8);
//
//		preface.add(new Paragraph(
//				"This document is a preliminary version and not subject to your license agreement or any other agreement with vogella.com ;-).",
//				redFont));

		document.add(preface);
	}
	 
	 private static void addEmptyLine(Paragraph paragraph, int number) {
		    for (int i = 0; i < number; i++) {
		      paragraph.add(new Paragraph(" "));
		    }
		  }

	/**
	 * @return the mapValuesPDF
	 */
	public Map<String, String> getMapValuesPDF() {
		return mapValuesPDF;
	}

	/**
	 * @param mapValuesPDF the mapValuesPDF to set
	 */
	public void setMapValuesPDF(Map<String, String> mapValuesPDF) {
		this.mapValuesPDF = mapValuesPDF;
	}

	

}
